package appreate.flags;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import appreate.flags.utils.Commons;
import appreate.flags.utils.CustomTypefaceSpan;

public class QuizSurvival extends SuperQuiz {

	private TextView mNomeNazione;
	private int mLife;
	private ImageView mProgress1;
	private ImageView mProgress2;
	private ImageView mProgress3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.survival);

		init();
		setFlags();
		setFlagWin();
		// setCounterTimer(true);

	}

	protected void setFlagWin() {
		double numberDouble = Math.random() * 4;
		int number = (int) numberDouble;

		if (mNameFlag4 == null && number == 3)
			number--;

		switch (number) {
		case 0:
			rightAswer = mNameFlag1;
			mNomeNazione.setText(getString(getResources().getIdentifier(mNameFlag1, "string",
					getApplicationInfo().packageName)));
			break;
		case 1:
			rightAswer = mNameFlag2;
			mNomeNazione.setText(getString(getResources().getIdentifier(mNameFlag2, "string",
					getApplicationInfo().packageName)));
			break;
		case 2:
			rightAswer = mNameFlag3;
			mNomeNazione.setText(getString(getResources().getIdentifier(mNameFlag3, "string",
					getApplicationInfo().packageName)));
			break;
		case 3:
			rightAswer = mNameFlag4;
			mNomeNazione.setText(getString(getResources().getIdentifier(mNameFlag4, "string",
					getApplicationInfo().packageName)));
			break;
		default:
			break;
		}

		history.remove(mNameFlag1);
		history.remove(mNameFlag2);
		history.remove(mNameFlag3);
		history.remove(mNameFlag4);
		history.add(rightAswer);

	}

	protected void setFlags() {
		mNameFlag1 = Commons.getInstance().searchFlag(this, history, mMode);
		if (mNameFlag1 == null) {
			decrLife(true);
		} else {
			history.add(mNameFlag1);
			int nameResourceID = getResources().getIdentifier(mNameFlag1, "drawable", getApplicationInfo().packageName);
			mButtonUno.setBackgroundResource(nameResourceID);
		}

		mNameFlag2 = Commons.getInstance().searchFlag(this, history, mMode);
		if (mNameFlag2 == null) {
			decrLife(true);
		} else {
			history.add(mNameFlag2);
			int nameResourceID = getResources().getIdentifier(mNameFlag2, "drawable", getApplicationInfo().packageName);
			mButtonDue.setBackgroundResource(nameResourceID);
		}
		mNameFlag3 = Commons.getInstance().searchFlag(this, history, mMode);
		if (mNameFlag3 == null) {
			decrLife(true);
		} else {
			history.add(mNameFlag3);
			int nameResourceID = getResources().getIdentifier(mNameFlag3, "drawable", getApplicationInfo().packageName);
			mButtonTre.setBackgroundResource(nameResourceID);
		}
		mNameFlag4 = Commons.getInstance().searchFlag(this, history, mMode);
		if (mNameFlag4 == null) {
			decrLife(true);
		} else {
			history.add(mNameFlag4);
			int nameResourceID = getResources().getIdentifier(mNameFlag4, "drawable", getApplicationInfo().packageName);
			mButtonQuattro.setBackgroundResource(nameResourceID);
		}
	}

	protected void init() {
		super.init();
		mLife = 3;
		mNomeNazione = (TextView) findViewById(R.id.nome_nazione);
		mProgress1 = (ImageView) findViewById(R.id.progress1);
		mProgress2 = (ImageView) findViewById(R.id.progress2);
		mProgress3 = (ImageView) findViewById(R.id.progress3);
		mNomeNazione.setTypeface(mTypeface);

	}

	@Override
	protected int getDrawableX(Button button) {
		button.setText("");
		return R.drawable.croix;

	}

	@Override
	protected int getDrawableY(Button button) {
		button.setText("");
		return R.drawable.valider;
	}

	@Override
	protected void decrLife(boolean zero) {
		mLife--;
		switch (mLife) {
		case 2:
			mProgress3.setVisibility(View.GONE);
			break;
		case 1:

			mProgress2.setVisibility(View.GONE);
			break;
		case 0:

			mProgress1.setVisibility(View.GONE);
			break;

		default:
			break;
		}
		if (zero)
			mLife = 0;

		if (mLife == 0) {
			final int scores = 3 * win - lose;

			String title = getString(R.string.score).replace("%score", scores + "");
			SpannableStringBuilder ssBuilser = new SpannableStringBuilder(title);
			CustomTypefaceSpan spanCustom = new CustomTypefaceSpan(Typeface.MONOSPACE.toString(), mTypeface);
			ssBuilser.setSpan(spanCustom, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

			String ok = "OK";
			SpannableStringBuilder ssBuilserOk = new SpannableStringBuilder(ok);
			ssBuilserOk.setSpan(spanCustom, 0, ok.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

			AlertDialog.Builder builder = new AlertDialog.Builder(QuizSurvival.this);
			builder.setMessage(ssBuilser).setCancelable(false)
					.setPositiveButton(ssBuilserOk, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							mScores = scores;
							saveRecord(3);
							QuizSurvival.this.finish();
						}
					});
			AlertDialog alert = builder.create();
			alert.show();
		}

	}

}